using System;
using System.Collections.Generic;
using System.Linq;
using UMC.Web;
using UMC.Security;
using UMC.Data;
using UMC.Web.UI;
using UMC.Wiki.Entities;

namespace UMC.Wiki.Activities
{
    class SubjectSubscribeActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                this.Context.Send("Login", true);
            }
            var code = this.AsyncDialog("code", r =>
            {
                return this.DialogValue("none");
            });


            var project = UMC.Wiki.DataFactory.Instance().Project(code);
            if (project == null)
            {
                var p = UMC.Data.Utility.UInt64Parse(code) ?? 0;
                if (p > 0)
                {
                    project = UMC.Wiki.DataFactory.Instance().Project(p);
                }
            }
            if (project == null)
            {
                this.Prompt("无此项目");

            }

            var member = UMC.Wiki.DataFactory.Instance().ProjectMember(project.Id.Value, user.Id.Value);

            if (member != null)
            {
                switch (member.AuthType)
                {
                    case WebAuthType.Admin:
                        break;
                    default:
                        this.Prompt("您不是此项目主，不可使用该功能");

                        break;
                }
            }
            else
            {
                this.Prompt("您不是此项目主，不可使用该功能");
            }


            var Model = this.AsyncDialog("Model", ml =>
            {


                var webr = UMC.Web.WebResource.Instance();
                var logoUrl = webr.ImageResolve(request.Url, project.Id.Value, "1", 4);

                var Discount = new UIHeader.Portrait(logoUrl);


                Discount.Value(project.Caption);
                Discount.Time(project.Description);

                var color = 0x63b359;
                Discount.Gradient(color, color);
                var header = new UIHeader();
                var title = UITitle.Create();

                title.Title = "知识变现";
                title.Style.BgColor(color);
                title.Style.Color(0xfff);
                header.AddPortrait(Discount, new UIStyle().Name("image-radius", "10"));

                var ui = UISection.Create(header, title);

                UIIconNameDesc uIIcon = new UIIconNameDesc();//
                uIIcon.Put("icon", '\uF0c0').Put("color", "#40c9c6").Put("name", "团队规模").Put("desc", project.MemberTotal + "人");

                ui.Add(uIIcon);

                uIIcon = new UIIconNameDesc(new UIIconNameDesc.Item('\uF02d', "文章数量", $"{project.SubjectTotal ?? 0}篇").Color(0x36a3f7), new UIIconNameDesc.Item('\uf06e', "浏览总数", $"{project.ViewTotal ?? 0}次").Color(0x34bfa3));
                ui.Add(uIIcon);
                // var expireTime = project.ExpireTime ?? 0;

                // var ui2 = ui.NewSection().AddCell("项目类型", expireTime == 0 ? "个人项目" : "企业项目");
                // if (expireTime > 0)
                // {
                //     ui2.AddCell("过期时间", Utility.Expire(expireTime));
                // }


                var ui4 = ui.NewSection().AddCell("订阅价格", $"{project.ReaderPrice ?? 0}元", new UIClick(request, ml, "ReaderPrice"));

                ui4.AddCell("可退天数", $"{project.TrialDays ?? 7}天", new UIClick(request, ml, "TrialDays"));
                ui4.AddCell("客服电话", project.Tel, new UIClick(request, ml, "Tel"));

                var status = project.Status ?? 0;

                ui4.AddCell("订阅方式", (status & 1) == 1 ? "快捷订阅" : "详情订阅", new UIClick(request, ml, "Status"));


                ui4.AddCell("评论开启", (status & 2) == 2 ? "关闭" : "开放", new UIClick(request, ml, "Comment"));


                var ls = ui.NewSection();
                var cell = UICell.UI("订阅说明", "", new UIClick(request, ml, "Content"));
                cell.Style.BorderAll();
                ls.Add(cell);

                var host = DataFactory.Instance().Content(project.Id.Value);


                if (String.IsNullOrEmpty(host?.Content) == false)
                {
                    ls.DisableSeparatorLine();
                    ls.AddCells(Markdown.Transform(host.Content));
                }
                else
                {
                    var uiDesc = new UIDesc(new UMC.Web.WebMeta().Put("desc", "未有介绍").Put("icon", "\uF016"));
                    uiDesc.Desc("{icon}\n{desc}");
                    uiDesc.Style.Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60));
                    ls.Add(uiDesc);
                }
                ui.SendTo(this.Context, $"{request.Model}.{request.Command}");
            });
            switch (Model)
            {
                case "ReaderPrice":
                    var ReaderPrice = Utility.IntParse(this.AsyncDialog("ReaderPrice", r =>
                    {
                        var dbF = new UINumberDialog(project.ReaderPrice) { Title = "项目订阅价" };
                        return dbF;
                    }), 0);

                    UMC.Wiki.DataFactory.Instance().Put(new Project { ReaderPrice = ReaderPrice, Id = project.Id });

                    break;
                case "Status":
                    {
                        var Status = Utility.IntParse(this.AsyncDialog("Status", r =>
                        {
                            var dbF = new UISheetDialog() { Title = "订阅方式" };
                            dbF.Put(new ListItem("详情订阅", "0"))
                            .Put(new ListItem("快捷订阅", "1"));
                            return dbF;
                        }), 0);
                        if (Status == 0)
                        {
                            var vStatis = project.Status ?? 0;
                            if ((vStatis & 1) == 1)
                            {
                                UMC.Wiki.DataFactory.Instance().Put(new Project { Status = vStatis ^ 1, Id = project.Id });

                            }
                        }
                        else
                        {
                            UMC.Wiki.DataFactory.Instance().Put(new Project { Status = (project.Status ?? 0) | 1, Id = project.Id });

                        }
                    }


                    break;
                case "Comment":
                    {
                        var Status = Utility.IntParse(this.AsyncDialog("Status", r =>
                        {
                            var dbF = new UISheetDialog() { Title = "评论设置" };
                            dbF.Put(new ListItem("开放评论", "0"))
                            .Put(new ListItem("关闭评论", "2"));
                            return dbF;
                        }), 0);
                        if (Status == 0)
                        {
                            var vStatis = project.Status ?? 0;
                            if ((vStatis & 2) == 2)
                            {
                                UMC.Wiki.DataFactory.Instance().Put(new Project { Status = vStatis ^ 2, Id = project.Id });

                            }
                        }
                        else
                        {
                            UMC.Wiki.DataFactory.Instance().Put(new Project { Status = (project.Status ?? 0) | 2, Id = project.Id });

                        }
                    }


                    break;
                case "Tel":
                    var Tel = this.AsyncDialog("Tel", r =>
                    {
                        var dbF = new UINumberDialog(project.Tel) { Title = "客服电话" };
                        return dbF;
                    });

                    UMC.Wiki.DataFactory.Instance().Put(new Project { Tel = Tel, Id = project.Id });

                    break;
                case "TrialDays":
                    var TrialDays = Utility.IntParse(this.AsyncDialog("TrialDays", r =>
                    {
                        var dbF = new UINumberDialog(project.TrialDays) { Title = "可退款的天数" };
                        return dbF;
                    }), 0);

                    UMC.Wiki.DataFactory.Instance().Put(new Project { TrialDays = TrialDays, Id = project.Id });

                    break;
                case "Content":
                    var Content = UIDialog.AsyncDialog(this.Context, "Content", r =>
                    {
                        var host = DataFactory.Instance().Content(project.Id.Value);
                        if (request.IsApp)
                        {
                            return new UIMarkdownDialog() { Title = "订阅说明", DefaultValue = host?.Content };
                        }
                        else
                        {
                            var dbF = new UIFormDialog() { Title = "订阅说明" };
                            dbF.AddTextarea("订阅说明", "Content", host?.Content).Put("Rows", 20).PlaceHolder("MD格式");
                            dbF.Submit("确认", $"{request.Model}.{request.Command}");
                            return dbF;

                        }
                    });
                    DataFactory.Instance().Put(new SubjectContent { Id = project.Id, Content = Content });

                    break;
                case "FollowPrice":
                    var FollowPrice = Utility.IntParse(UIDialog.AsyncDialog(this.Context, "FollowPrice", r =>
                    {
                        var dbF = new UIFormDialog() { Title = "项目撮合价" };
                        dbF.AddNumber("自动撮合价", "FollowPrice", (project.FollowPrice ?? 0) / 10).PlaceHolder("单位为角");
                        dbF.AddFooter("单位为角,当广告主出的价格高于此价,则自动撮合;价格为零时,则关闭自动撮合.");
                        dbF.Submit("确认", $"{request.Model}.{request.Command}");
                        return dbF;
                    }), 0);

                    UMC.Wiki.DataFactory.Instance().Put(new Project { FollowPrice = FollowPrice * 10, Id = project.Id });

                    break;
            }
            this.Context.Send($"{request.Model}.{request.Command}", true);

        }


    }

}